package com.llk.pool.controller.admin;

import com.google.gson.Gson;
import com.llk.pool.controller.BaseController;
import com.llk.pool.domain.DataVO;
import com.llk.pool.domain.User;
import com.llk.pool.mapper.UserMapper;
import com.llk.pool.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.xml.crypto.Data;
import java.util.*;

/**
 * Created by zhou on 2017/5/5.
 */
@Controller()
@RequestMapping("/admin/user")
public class AdminUserController extends BaseController{


    public AdminUserController() {
        super(AdminUserController.class);
    }

    @Autowired
    private UserService userService;




    @ApiIgnore
    @GetMapping(value = "index")
    public String index(){
        return "admin/user/index";
    }


    @ApiOperation( value = "获取所有用户",notes = "admin后台管理接口，获取用户列表")
    @GetMapping("list")
    @ResponseBody
    public String  users(HttpServletRequest request){


//        System.out.println("*********************************");
//        Map<String, String[]> map = request.getParameterMap();
//
//        Iterator<String> iterator = map.keySet().iterator();
//        while(iterator.hasNext()){
//            String keyTem = iterator.next();
//            String[] valueTem = map.get(keyTem);
//            System.out.println("parameter: " + keyTem + "----value:" + Arrays.toString(valueTem));
//
//        }
//
//
//        System.out.println("*********************************");

        String[] columnsName = {"id", "userName", "nickName", "userSex"};

        int pageSize = 10;
        int startRecord = 0;
        String size = request.getParameter("length");
        if (!"".equals(size) && size != null) {
            pageSize = Integer.parseInt(size);
        }
        String currentRecord = request.getParameter("start");
        if (!"".equals(currentRecord) && currentRecord != null) {
            startRecord = Integer.parseInt(currentRecord);
        }
        // For sortable
        String sortOrder = request.getParameter("order[0][column]");
        String sortDir = request.getParameter("order[0][dir]");
        System.out.println("sortOrder: " + sortOrder);
        System.out.println("sortDir: " + sortDir);

        // For search
        String searchValue = request.getParameter("search[value]");
        int count = 0;
        List<User> results = new ArrayList<User>();

        count = userService.count();
        results = userService.loadUserList(pageSize, startRecord, columnsName[Integer.parseInt(sortOrder)], sortDir, searchValue);
        DataVO<User> result = new DataVO<User>();
        result.setDraw(Integer.parseInt(request.getParameter("draw") == null ? "0"
                : request.getParameter("draw")) + 1);
        result.setData(results);
        result.setRecordsTotal(count);
        result.setRecordsFiltered(count);



        Gson gson = new Gson();
        String rejson = gson.toJson(result);

        System.out.println(rejson);


        return  rejson;
    }
}
